<?php
function array_length($arr){
    $length = 0;
    foreach($arr as $key =>$value){
            $length++;
    }
    return $length;
}
function array_implode($arr,$needle=','){
    $length = array_length($arr);
    $str = $needle;
    $i = 0;
    while($i<$length){
        $inst = $arr[$i]; 
        //$str .=$interests[$i];
        $str = $str . $inst.$needle;
        $i++;
    }
    return $str;
}
$hasError=false; 
//检查用户名
//必须填写
//至少三个字符
//最多五个字符
//字母开头,字母数字下划线
$username = trim($_POST["Username"]);
if(!$username) {
    echo "用户名必须填写<br/>";
    $hasError = true;
}else {
    $len = strlen($username);
    if($len<2){
        echo "用户名要有二个字符<br/>";
        $hasError = true;
    }
}


//检查密码
//必须填写
//至少三个字符
//最多五个字符

$password = trim($_POST["Password"]);
if(!$password) {
    echo "密码必须填写<br/>";
    $hasError=true;
}else {
    $len = strlen($password);
    if($len<2){
        echo "密码至少二个字符<br/>";
        $hasError = true;
    }else if($len>12){
        echo "密码最多十二个字符<br/>";
        $hasError = true;
    }
}



$phone = $_POST['Phone'];
if($phone){
    // '/^\d+$/'
    $Phone = intval($phone);
    if(!$Phone || $Phone <=0 || $Phone>19999999999){
        echo "请填写正确的电话号码<br/>";
        $hasError = true;
    }

}

$mail = trim($_POST["Mail"]);
if(!$mail) {
    $len = strlen($mail);
    if($len<5){
        echo "邮箱至少五个字符<br/>";
        $hasError = true;
    }else if($len>20){
        echo "邮箱最多二十个字符<br/>";
        $hasError = true;
    }
}




if(!$hasError){
    $sql = "INSERT INTO `account` (`Username`,`Password`,`Mail`,`Phone`)VALUES('$username','$password','$mail','$phone')";
    $sqli = new mysqli("localhost","root","","mybolg");
    $sqli ->query($sql);
}



$url = "login.php";
Header("Location: $url");

?>

